<template>
  <el-row type="flex" style="width:100%" justify="space-between">
    <el-col>{{ treeNode.name }}</el-col>
    <el-col :span="4">
      <el-row type="flex" justify="end">
        <el-col>{{ treeNode.manager }}</el-col>
        <el-col>
          <!-- 下拉菜单 -->
          <el-dropdown @command="operateDepts">
            <span>操作</span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item command="add">添加子部门</el-dropdown-item>
              <el-dropdown-item v-if="!isRoot" command="edit">编辑部门</el-dropdown-item>
              <el-dropdown-item v-if="!isRoot" command="del">删除部门</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
          <!-- 下拉菜单/ -->
        </el-col>
      </el-row>
    </el-col>
  </el-row>
</template>

<script>
import { delDepartments } from '@/api/departments'
// 组件封装原则- 复用的结构 - 别人用你的组件给你传参数 props
export default {
  // props: ['treeNode']
  props: {
    treeNode: {
      type: Object,
      required: true
    },
    isRoot: {
      type: Boolean,
      default: false
    }
  },
  methods: {
    operateDepts(type) {
      if (type === 'add') {
        // 添加
        this.$emit('addDepts', this.treeNode)
      } else if (type === 'edit') {
        // 编辑
        this.$emit('editDepts', this.treeNode)
      } else {
        // 删除
        this.$confirm('是否确认删除').then(() => {
          return delDepartments(this.treeNode.id)
        }).then(() => {
          // 以为只删除成功
          this.$emit('delDepts')
          this.$message.success('删除部门成功')
        }).catch(() => {})
      }
    }
  }
}
</script>

<style>

</style>
